import modelEnhance from '@/utils/modelEnhance';
import PageHelper from '@/utils/pageHelper';
import querystring from 'querystring';

let LOADED = false;
export default modelEnhance({
  namespace: 'parameter',
  state: {
    pageData: PageHelper.create(),
    recordId: 0,
    product: {}
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/product/parameter' && !LOADED) {
          LOADED = true;
          // 获取路由参数
          dispatch({
            type: 'setRecordId',
            payload: { recordId: querystring.parse(history.location.search.replace('?', '')).id }
          });
          dispatch({
            type: 'init'
          });
        }
      });
    }
  },
  effects: {
    // 进入页面加载
    *init({ payload }, { call, put, select }) {
      const { pageData } = yield select(state => state.parameter);
      yield put({
        type: 'getPageInfo',
        payload: {
          pageData: pageData.startPage(1, 10),
        }
      });
      yield put({
        type: 'getProduct'
      });
    },
    *getProduct({ payload }, { call, put, select }) {
      const { recordId } = yield select(state => state.parameter);
      yield put({
        type: '@request',
        payload: {
          valueField: 'product',   //存放返回的数据
          url: '/product/' + recordId + '/',
          method: 'get'
        },
        afterResponse: (resp) => resp.data,
      })
    },
    // 获取分页数据
    *getPageInfo({ payload }, { call, put, select }) {
      // 获取state数据
      const { recordId } = yield select(state => state.parameter);
      const { pageData } = payload;
      yield put({
        type: '@request',
        payload: {
          valueField: 'pageData',
          // url: '/product/parameter/?house=' + recordId,
          url: '/product/parameter/',
          // pageInfo: pageData,
          method: 'get'
        }
      });
    },
    *save({ payload }, { call, put, select, take }) {
      const { values, record, success } = payload;
      const { pageData, recordId } = yield select(state => state.parameter);
      // put是非阻塞的 put.resolve是阻塞型的
      yield put.resolve({
        type: '@request',
        payload: {
          notice: true,
          url: record ? `/product/parameter/${record.id}/` : '/product/parameter/',
          data: values,
          method: record ? 'patch' : 'post',
          afterResponse: (resp) => {
            success()
          }
        }
      });
      success();
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
    },
    *update({ payload }, { call, put }) { },
    // 删除 之后查询分页
    *remove({ payload }, { call, put, select }) {
      const { records, success } = payload;
      const { pageData } = yield select(state => state.parameter);
      yield put.resolve({
        type: '@request',
        payload: {
          notice: true,
          url: `/product/parameter/${records[0].id}/`,
          method: 'delete'
        }
      });
      success();
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
    },
  },
  reducers: {
    setRecordId(state, { payload }) {
      const { recordId } = payload
      let _state = state;
      _state.recordId = recordId
      return state
    }
  }
});